#define _CRT_SECURE_NO_WARNINGS

#include"BinaryTree.h"


BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
	if (*pi >= n || a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}

	BTNode* root = (BTNode*)malloc(sizeof(BTNode));
	if (root == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	root->data = a[*pi];

	(*pi)++;
	root->left = BinaryTreeCreate(a, n, pi);
	root->right = BinaryTreeCreate(a, n, pi);
	return root;
}

void BinaryTreeDestory(BTNode** root)
{
	assert(root);
	if (*root == NULL)
	{
		return;
	}

	BinaryTreeDestory(&((*root)->left));
	BinaryTreeDestory(&((*root)->right));

	free(*root);
	*root = NULL;
}

int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;

	if (root->left == NULL && root->right == NULL)
		return 1;

	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

int TreeHeight(BTNode* root)
{
	if (root == NULL)
		return 0;

	int leftHeight = TreeHeight(root->left);
	int rightHeight = TreeHeight(root->right);

	return leftHeight > rightHeight ?
		leftHeight + 1 : rightHeight + 1;
}

void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("#");
		return;
	}

	printf("%c", root->data);
	BinaryTreePrevOrder(root->left);
	BinaryTreePrevOrder(root->right);
	
}

void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("#");
		return;
	}

	BinaryTreeInOrder(root->left);
	printf("%c", root->data);
	BinaryTreeInOrder(root->right);

}

void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("#");
		return;
	}

	BinaryTreePostOrder(root->left);
	BinaryTreePostOrder(root->right);
	printf("%c", root->data);
}

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{

	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
		return root;

	BTNode* left = BinaryTreeFind(root->left, x);
	if (left)
		return left;
	
	return BinaryTreeFind(root->right, x);
}

int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (k == 1)
		return 1;
	if (root == NULL && k != 1)
		return 0;
	int left=0, right=0;
	if (root->left)
		left = BinaryTreeLevelKSize(root->left, k - 1);
	if(root->right)
		right = BinaryTreeLevelKSize(root->right, k - 1);
	return left + right;
}

void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q,root);
	while (!QueueEmpty(&q))
	{
		BTNode* cur = QueueFront(&q);
			printf("%c", cur->data);
			QueuePop(&q);
			if(cur->left)
			QueuePush(&q, cur->left);
			if (cur->right)
			QueuePush(&q, cur->right);
	}

	QueueDestory(&q);
}

int BinaryTreeComplete(BTNode* root)
{
	if (root == NULL)
		return 1;
	if (root->left != NULL && root->right == NULL)
		return 0;
	if (root->left == NULL && root->right!=NULL)
		return 0;
	if (root->left && root->right)
	{
		int left = BinaryTreeComplete(root->left);
		int right = BinaryTreeComplete(root->right);
		return left + right == 2 ? 1 : 0;
	}

	else
		return 1;
}